package com.gss.armory.persistence;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import com.gss.armory.domain.BusinessUnitTypeDTO;
import com.gss.armory.domain.CompanyDetailsDTO;
import com.gss.armory.domain.DepartmentDetailsDTO;
import com.gss.armory.domain.DocGeneralDetailsDTO;
import com.gss.armory.domain.DocumentCategoryDTO;
import com.gss.armory.domain.DocumentSubCategory;
import com.gss.armory.domain.DocumentTypeDTO;
import com.gss.armory.domain.LocationDetailsDTO;
import com.gss.armory.domain.MasterDetailsForDMSWFDTO;
import com.gss.security.domain.LocationMasterDTO;
import com.gss.security.domain.SuperficialRolesMapping;
import com.gss.security.domain.UserDetailsDTO;
import com.gss.security.domain.UserRoleMappingDTO;

@Repository
public class AdminDao extends HibernateDaoSupport {

	private Logger logger = Logger.getLogger(AdminDao.class);

	@Autowired
	public void init(SessionFactory sessionFactory) {
		super.setSessionFactory(sessionFactory);
	}

	public boolean saveDataInTable(Object entity) {
		boolean result = false;
		try{
			getHibernateTemplate().save(entity);
			result = true;
		}
		catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return result;
	}
	
	public boolean saveCompany(CompanyDetailsDTO entity) {
		try {
			getHibernateTemplate().save(entity);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return false;
		}
	}

	public boolean saveCompanyEditDetails(CompanyDetailsDTO entity) {
		try {
			getHibernateTemplate().update(entity);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return false;
		}
	}

	public boolean saveLocation(LocationDetailsDTO detailsDTO) {
		try {
			getHibernateTemplate().save(detailsDTO);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return false;
		}
	}

	public boolean saveDepartment(DepartmentDetailsDTO detailsDTO) {
		try {
			getHibernateTemplate().save(detailsDTO);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return false;
		}

	}

	public boolean saveUser(UserDetailsDTO detailsDTO) {

		try {
			getHibernateTemplate().save(detailsDTO);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return false;
		}

	}

	public boolean saveDocumentType(DocumentTypeDTO entity) {
		try {
			getHibernateTemplate().save(entity);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return false;
		}
	}

	public boolean saveDocumentCategory(DocumentCategoryDTO entity) {
		try {
			getHibernateTemplate().save(entity);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return false;
		}
	}

	public void loadDocumentType(DocumentTypeDTO dcParentType) throws Exception {
		getHibernateTemplate().load(dcParentType, dcParentType.getDtId());
	}

	public boolean saveDocumentSubCategory(
			DocumentSubCategory documentSubCategory) {
		try {
			getHibernateTemplate().save(documentSubCategory);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return false;
		}
	}

	public boolean updateUser(UserRoleMappingDTO mappingDTO) {
		try {
			getHibernateTemplate().saveOrUpdate(mappingDTO);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return false;
		}
	}

	public boolean insertDetailsInMasterDetailsTable(
			MasterDetailsForDMSWFDTO masterDetailsForDMSWFDTO) {

		try {
			getHibernateTemplate().save(masterDetailsForDMSWFDTO);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return false;
		}
	}

	public boolean saveLocationMasterDetails(LocationMasterDTO locationMasterDTO) {
		try {
			getHibernateTemplate().saveOrUpdate(locationMasterDTO);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return false;
		}

	}

	public boolean saveUnitTypeEditDetails(
			BusinessUnitTypeDTO businessUnitTypeDTO) {
		try {
			getHibernateTemplate().update(businessUnitTypeDTO);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return false;
		}
	}

	public boolean saveUnitType(BusinessUnitTypeDTO entity) {
		try {
			getHibernateTemplate().save(entity);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return false;
		}

	}

	public boolean saveDepartmentEditDetails(
			DepartmentDetailsDTO departmentDetailsDTO) {

		try {
			getHibernateTemplate().update(departmentDetailsDTO);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return false;
		}

	}

	public boolean saveBusinessUnitEditDetails(
			LocationDetailsDTO locationDetailsDTO) {

		try {
			getHibernateTemplate().update(locationDetailsDTO);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return false;
		}
	}

	public CompanyDetailsDTO getCompanyDetailsObject(
			CompanyDetailsDTO companyDetailsDTO) throws Exception {

		getHibernateTemplate().load(companyDetailsDTO,
				companyDetailsDTO.getCdId());
		return companyDetailsDTO;

	}

	public boolean saveEditedUSerDetails(UserDetailsDTO userDetailsDTO) {

		try {
			getHibernateTemplate().update(userDetailsDTO);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return false;
		}
	}

	public boolean updateTypeDetails(DocumentTypeDTO documentTypeDTO) {

		try {
			getHibernateTemplate().update(documentTypeDTO);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return false;
		}

	}

	public boolean updateCategoryDetails(DocumentCategoryDTO documentCategoryDTO) {

		try {

			getHibernateTemplate().update(documentCategoryDTO);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return false;
		}

	}

	public boolean updateDocSubCategDetails(
			DocumentSubCategory documentSubCategory) {
		try {
			getHibernateTemplate().update(documentSubCategory);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return false;
		}
	}

	public boolean deleteSelectedCompany(CompanyDetailsDTO companyDetailsDTO) {
		try {
			getHibernateTemplate().delete(companyDetailsDTO);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return false;
		}
	}

	public List<UserRoleMappingDTO> usersListForRoleId(
			DetachedCriteria detachedCriteria) {
		try {
			return getHibernateTemplate().findByCriteria(detachedCriteria);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return null;
		}
	}

	public boolean saveSuperficialMappingDetails(
			List<SuperficialRolesMapping> listOfRoleMappingObjects) {
		boolean resultOfSave = false;
		try {
			getHibernateTemplate().saveOrUpdateAll(listOfRoleMappingObjects);
			resultOfSave = true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return resultOfSave;
	}

	public boolean deleteDataForSuperFicialRoleMapping(
			SuperficialRolesMapping superficialRolesMapping) {
		boolean resultOfDelete = false;

		try {
			getHibernateTemplate().delete(superficialRolesMapping);
			resultOfDelete = true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return resultOfDelete;

	}

	public List fetchDataForSuperFicialRoleMapping(
			DetachedCriteria detachedCriteria) {

		return getHibernateTemplate().findByCriteria(detachedCriteria);

	}

	public List<DocGeneralDetailsDTO> loadDocument(String docCode) {
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(DocGeneralDetailsDTO.class);
		detachedCriteria.add(Restrictions.eq("docId", Long.parseLong(docCode)));
		try {
			return getHibernateTemplate().findByCriteria(detachedCriteria);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return null;

	}

	public boolean deletePreviousRoles(UserRoleMappingDTO mappingDTO) {
		boolean resultOfDelete = false;
		try {
			getHibernateTemplate().delete(mappingDTO);
			resultOfDelete = true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return resultOfDelete;
	}

	public boolean updateNewUserDetails(UserDetailsDTO detailsDTO) {
		boolean isSuccess = false;
		try {
			getHibernateTemplate().update(detailsDTO);
			isSuccess = true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return isSuccess;

	}

	public UserDetailsDTO migrateUserauthDetailstoUserDetails(
			String userNameFromAuthObject) {
		logger.debug("inside admin dao: method(migrateUserauthDetailstoUserDetails): getting details for: "
				+ userNameFromAuthObject);

		UserDetailsDTO userDetailsDTO = new UserDetailsDTO();
		userDetailsDTO = getHibernateTemplate().load(UserDetailsDTO.class,
				userNameFromAuthObject);
		return userDetailsDTO;
	}
	
	public UserDetailsDTO fetchUserDetailsForUserId(String strUserId){
		logger.debug("inside authentication dao: method(fetchUserDetailsForUserId): ");
		
		UserDetailsDTO userDetailsDTO = new UserDetailsDTO();
		getHibernateTemplate().load(userDetailsDTO, strUserId);
		logger.debug("UserDetailsDTO Object for user id:" + strUserId + " is: " + userDetailsDTO.toString());
		return userDetailsDTO;
	}

	public DocGeneralDetailsDTO loadDocumentForId(long docId) {
		return getHibernateTemplate().get(DocGeneralDetailsDTO.class, docId);
	}
}
